package com.stock.filter;

import com.stock.util.PointValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PointInterceptor implements HandlerInterceptor {

    @Autowired
    private PointValidatorUtil pointValidatorUtil;

    // 定义需要积分校验和扣除的路径
    private static final String[] EXCLUDED_PATHS = {"/stockData"};

    /**
     * 在请求处理之前进行拦截
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        // 判断当前请求是否匹配需要积分校验和扣除的路径
        for (String path : EXCLUDED_PATHS) {
            if (requestURI.startsWith(path)) {
                // 校验并扣除积分
                pointValidatorUtil.checkAndDeductCurrentUserPoint();
            }
        }
        // 返回 true，表示继续执行后续的处理
        return true;
    }

}
